//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows.Markup;
using System.Windows.Media;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing
{
    internal class CustomGeometry2D : Geometry
    {
        private ObservableCollection<AdjustHandle> ahLst;
        private ObservableCollection<GeometryGuide> avLst;
        private ObservableCollection<ConnectionSite> cxnLst;

        private ObservableCollection<GeometryGuide> gdLst;
        private ObservableCollection<Path2D> pathLst;
        private GeometryRectangle rect;

        public ObservableCollection<GeometryGuide> AdjustValueList
        {
            get
            {
                if (avLst == null)
                    avLst = new ObservableCollection<GeometryGuide>();
                return avLst;
            }
            set { avLst = value; }
        }

        public ObservableCollection<GeometryGuide> Guides
        {
            get { return gdLst; }
            set { gdLst = value; }
        }

        public ObservableCollection<AdjustHandle> AdjustHandles
        {
            get { return ahLst; }
            set { ahLst = value; }
        }

        public ObservableCollection<ConnectionSite> Connexions
        {
            get { return cxnLst; }
            set { cxnLst = value; }
        }

        public GeometryRectangle Rectangle
        {
            get { return rect; }
            set { rect = value; }
        }

        public ObservableCollection<Path2D> Paths
        {
            get { return pathLst; }
            set { pathLst = value; }
        }

        internal override System.Windows.Media.Geometry GetWPFGeometry(ShapeProperties shape, bool arrows)
        {
            PathGeometry g = new PathGeometry();
            g.FillRule = FillRule.EvenOdd;
            foreach (Path2D path in pathLst)
            {
                g.Figures.Add(path.PathFigure);
#if !SILVERLIGHT
                Debug.WriteLine(XamlWriter.Save(g));
#endif
                break; //\\check this
            }
            return g;
        }


        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("custGeom"))
            {
                avLst = c.ReadOptionalCollection<GeometryGuide>("avLst", "gd");
                gdLst = c.ReadOptionalCollection<GeometryGuide>("gdLst", "gd");
                if (c.ReaderLocalName == "ahLst")
                {
                    ahLst = new ObservableCollection<AdjustHandle>();
                    using (c.Read("ahLst"))
                    {
                        AdjustHandle h = AdjustHandle.ReadOptionalAdjustHandle(c);
                        while (h != null)
                        {
                            ahLst.Add(h);
                            h = AdjustHandle.ReadOptionalAdjustHandle(c);
                        }
                    }
                }
                else
                    ahLst = null;
                Connexions = c.ReadOptionalCollection<ConnectionSite>("cxnLst", "cxn");
                Rectangle = c.ReadOptionalElement<GeometryRectangle>("rect");
                pathLst = c.ReadOptionalCollection<Path2D>("pathLst", "path");
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("custGeom"))
            {
                c.WriteOptionalElementList(avLst, "avLst", true);
                c.WriteOptionalElementList(gdLst, "gdLst", true);
                c.WriteOptionalElementList(ahLst, "ahLst", true);
                c.WriteOptionalElementList(Connexions, "cxnLst", true);
                c.WriteOptionalElement(Rectangle);
                c.WriteOptionalElementList(pathLst, "pathLst", true);
            }
        }
    }
}